exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.BaseController {
  /**
     * 查询事件分类列表
     *
     */
  eventGroupAllAction() {
    var _this = this;

    return _asyncToGenerator(function* () {
      const EventGroupService = _this.service('cms/EventGroupService');
      const res = yield EventGroupService.eventGroupAll();
      return _this.json(res);
    })();
  }
  /**
     * 分页查询事件分类列表
     */
  eventGroupListAction() {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      const data = {
        page: Number(_this2.get('page')),
        pageSize: Number(_this2.get('pageSize'))
      };
      if (_this2.get('group_name')) {
        data.group_name = _this2.get('group_name');
      }
      if (_this2.get('id')) {
        data.id = Number(_this2.get('id'));
      }
      const aj = think.ajaxJson();
      const EventGroupService = _this2.service('cms/EventGroupService');
      const res = yield EventGroupService.getEventGroupList(data);
      if (!res.error) {
        aj.result = res;
      } else {
        aj.success = false;
        aj.msg = '获取事件分类列表失败';
      }
      return _this2.json(aj);
    })();
  }
  /**
     * 删除事件分类
     */
  delEGInfoAction() {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      const data = {
        id: _this3.post('id')
      };
      const aj = think.ajaxJson();
      const EventGroupService = _this3.service('cms/EventGroupService');
      const res = yield EventGroupService.delEGInfo(data);
      if (res && res.error) {
        aj.success = false;
        aj.msg = '事件分类删除错误';
      }
      if (res && res.msg) {
        // 有子类
        aj.success = false;
        aj.msg = res.msg;
      }
      return _this3.json(aj);
    })();
  }

  /**
     * 保存事件分类信息
     */
  saveEGInfoAction() {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      const file = _this4.file('image');
      const EventGroupService = _this4.service('cms/EventGroupService');
      const saveData = _this4.post('group_name,remark');
      const aj = think.ajaxJson();
      const checkData = {
        group_name: _this4.post('group_name'),
        group_code: _this4.post('group_code')
      };
      if (_this4.post('id')) {
        // 编辑
        checkData.id = _this4.post('id');
        saveData.id = _this4.post('id');
        if (!saveData.remark) {
          saveData.remark = null;
        }
      } else {
        saveData.group_code = _this4.post('group_code');
        saveData.group_level = Number(_this4.post('group_level'));
        if (saveData.group_level === 2) {
          saveData.parent_id = _this4.post('parent_id');
        }
      }
      const check = yield EventGroupService.checkCode(checkData);
      if (check > 0) {
        aj.success = false;
        aj.msg = '事件分类名称或分类代码已存在，请重新确认';
      } else {
        if (_this4.post('group_pic_path') && file) {
          // 删除原图
          // console.log('删除图片');
          const delArr = [_this4.post('group_pic_path')];
          const del = yield _this4.delFile(delArr);
          if (!del) {
            aj.success = false;
            aj.msg = '原有图标删除错误';
            return _this4.json(aj);
          }
        }
        if (file) {
          // 保存图片
          // console.log('保存图片');
          const pathObj = yield _this4.saveFile(file, 'static/upload/eventIcons/');
          if (pathObj) {
            saveData.group_pic_path = pathObj.filePathStatic;
          } else {
            aj.success = false;
            aj.msg = '图片保存错误';
            return _this4.json(aj);
          }
        }
        const res = yield EventGroupService.saveEGInfo(saveData);
        if (res.error) {
          aj.success = false;
          aj.msg = '事件分类保存失败';
        }
      }
      return _this4.json(aj);
    })();
  }
};